﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>#Warn - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The #Warn directive enables or disables warnings for specific conditions which may indicate an error, such as a typo or missing &quot;global&quot; declaration." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>#Warn</h1>

<p>Enables or disables warnings for specific conditions which may indicate an error, such as a typo or missing "global" declaration.</p>

<pre class="Syntax"><span class="func">#Warn</span> <span class="optional">WarningType, WarningMode</span></pre>

<h2 id="Parameters">参数</h2>
<dl>

  <dt>WarningType</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>

    <p>The type of warning to enable or disable. If omitted, it defaults to <i>All</i>.</p>
	    <p class="warning"><strong>Note:</strong> The UseUnset warnings are enabled by default. Disabling them is not recommended as there are plans to remove the option and replace the warning with an error.</p>
    <p id="UseUnset"><strong>UseUnsetLocal</strong> or <strong>UseUnsetGlobal</strong>: Warn when a variable is read without having previously been assigned a value or initialized with <a href="VarSetStrCapacity.htm">VarSetStrCapacity</a>. If the variable is intended to be empty, assign an empty string to suppress this warning.</p>
    <p><a href="IsSet.htm">IsSet</a> can be used to detect whether a variable has been set without triggering a warning, regardless of whether the warning is enabled.</p>
    <p>For the MsgBox mode, only one warning is displayed for each global variable, or for each local variable each time the function is called. This is achieved by initializing the variable when the warning is displayed, so subsequent calls to <a href="IsSet.htm">IsSet</a> are also affected.</p>
    <p>This is split into separate warning types for locals and globals because it is more common to use a global variable without prior initialization, due to their persistent and script-wide nature. For this reason, some script authors may wish to enable this type of warning for locals but disable it for globals.</p>
    <pre>#Warn
<em>;y := ""  ; This would suppress the warning.</em>
x := y    <em>; y hasn't been assigned a value.</em></pre>
    <p id="LocalSameAsGlobal"><strong>LocalSameAsGlobal</strong>: Before the script starts to run, display a warning for each <em>undeclared</em> local variable which has the same name as a global variable. This is intended to prevent errors caused by forgetting to declare a global variable inside a function before attempting to access it. If the variable really was intended to be local, a declaration such as <code>local x</code> or <code>static y</code> can be used to suppress the warning. This warning is never shown for variables inside a <a href="../Functions.htm#ForceLocal">force-local</a> function.</p>
    <pre>#Warn
g := 1
ShowG() {       <em>; The warning is displayed even if the function is never called.</em>
    <em>;global g   ; &lt;-- This is required to access the global variable.</em>
    MsgBox g  <em>; Without the declaration, "g" is an empty local variable.</em>
}</pre>
    <p id="Unreachable"><strong>Unreachable</strong>: Before the script starts to run, show a warning for each line that immediately follows a <code>Return</code>, <code>Break</code>, <code>Continue</code>, <code>Throw</code> or <code>Goto</code> at the same nesting level, unless that line is the target of a label. Any such line would never be executed.</p>
    <p>If the code is intended to be unreachable - such as if a <code>return</code> has been used to temporarily disable a block of code, or a hotkey or hotstring has been temporarily disabled by commenting it out - consider commenting out the unreachable code as well. Alternatively, the warning can be suppressed by defining a <a href="../misc/Labels.htm">label</a> above the first unreachable line.</p>
    <p><strong>All</strong>: Apply the given <em>WarningMode</em> to all supported warning types.</p>
  </dd>

  <dt>WarningMode</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>

    <p>A value indicating how warnings should be delivered. If omitted, it defaults to <em>MsgBox</em>.</p>
    <p><strong>MsgBox</strong>: Show a message box describing the warning. Note that once the message box is dismissed, the script will continue as usual.</p>
    <p><strong>StdOut</strong>: Send a description of the warning to <em>stdout</em> (the program's standard output stream), along with the filename and line number. This allows fancy editors such as SciTE to capture warnings without disrupting the script - the user can later jump to each offending line via the editor's output pane.</p>
    <p><strong>OutputDebug</strong>: Send a description of the warning to the debugger for display. If a debugger is not active, this will have no effect. 有关详细信息, 请参阅 <a href="OutputDebug.htm">OutputDebug</a>.</p>
    <p><strong>Off</strong>: Disable warnings of the given <em>WarningType</em>.</p>
  </dd>

</dl>
<h2 id="Remarks">备注</h2>
	<p>By default, all warnings are off.</p>
	<p>Warnings can't be enabled or disabled at run-time; the settings are determined when a script loads.  Therefore, the location in the script is not significant (and, like other # directives, #Warn cannot be executed conditionally).</p>
	<p>However, the ordering of multiple #Warn directives is significant: the last occurrence that sets a given warning determines the mode for that warning.  So, 例如, the two statements below have the combined effect of enabling all warnings except UseUnsetGlobal:</p>
<pre>#Warn All
#Warn UseUnsetGlobal, Off
</pre>
<h2 id="Related">相关</h2>
<p><a href="../Functions.htm#Local">Local and Global Variables</a></p>
<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>
#Warn All, Off                    <em>; Disable all warnings.  This is the default state.</em>
#Warn                             <em>; Enable every type of warning; show each warning in a message box.</em>
#Warn UseUnsetLocal, OutputDebug  <em>; Warn when a local variable is used before it's set; send warning to OutputDebug.</em>
</pre>
</div>

</body>
</html>